using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace BlimpControlPanel
{
   public partial class SetupForm : Form
   {
      private SerialPort thisGPSPort = null;

      public SetupForm(SerialPort gpsPort)
      {
         InitializeComponent();

         thisGPSPort  = gpsPort;

         cbPortName.Items.AddRange(SerialPort.GetPortNames());
         cbPortName.SelectedItem = gpsPort.PortName;
         cbDataBits.SelectedItem = gpsPort.DataBits.ToString();
         cbBaudRate.SelectedItem = gpsPort.BaudRate.ToString();

         UpdateHandshakeCombobox();
         UpdateParityCombobox();
         UpdateStopBitsCombobox();
      }

      private void UpdateStopBitsCombobox()
      {
          SetupStopBits(cbStopBits);
          cbStopBits.SelectedItem = thisGPSPort.StopBits;
      }

      private void SetupStopBits(ComboBox cb)
      {
          cb.Items.Add(StopBits.None);
          cb.Items.Add(StopBits.One);
          cb.Items.Add(StopBits.OnePointFive);
          cb.Items.Add(StopBits.Two);
      }

      private void UpdateParityCombobox()
      {
          SetupParity(cbParity);
          cbParity.SelectedItem = thisGPSPort.Parity;
      }

      private void SetupParity(ComboBox cb)
      {
          cb.Items.Add(Parity.Even);
          cb.Items.Add(Parity.Mark);
          cb.Items.Add(Parity.None);
          cb.Items.Add(Parity.Odd);
          cb.Items.Add(Parity.Space);
      }

      private void UpdateHandshakeCombobox()
      {
          SetupHandshake(cbHandshake);
          cbHandshake.SelectedItem = thisGPSPort.Handshake;
      }

      private void SetupHandshake(ComboBox cb)
      {
          cb.Items.Add(Handshake.None);
          cb.Items.Add(Handshake.RequestToSend);
          cb.Items.Add(Handshake.RequestToSendXOnXOff);
          cb.Items.Add(Handshake.XOnXOff);
      }

      public SerialPort GetUpdatedSerialPort()
      {
          thisGPSPort.BaudRate  = int.Parse(cbBaudRate.Items[cbBaudRate.SelectedIndex].ToString());
          thisGPSPort.DataBits  = int.Parse(cbDataBits.Items[cbDataBits.SelectedIndex].ToString());
          thisGPSPort.StopBits  = (StopBits)cbStopBits.SelectedItem;
          thisGPSPort.Handshake = (Handshake)cbHandshake.SelectedItem;
          thisGPSPort.Parity    = (Parity)cbParity.SelectedItem;
          thisGPSPort.PortName  = (string)cbPortName.SelectedItem;
          return thisGPSPort;
      }

      private void SetupForm_Load(object sender, EventArgs e)
      {

      }

      private void btnOK_Click(object sender, EventArgs e)
      {

      }

      private void gbXBee_Enter(object sender, EventArgs e)
      {

      }
   }
}